package com.have.blog.xo.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.model.api.CommonPage;
import com.have.blog.model.mapper.TAdminMapper;
import com.have.blog.model.mapper.TRoleMapper;
import com.have.blog.model.models.TAdmin;
import com.have.blog.model.models.TAdminExample;
import com.have.blog.model.models.TRole;
import com.have.blog.model.models.TRoleExample;
import com.have.blog.utils.MyStringUtils;
import com.have.blog.utils.ResultUtils;
import com.have.blog.xo.constant.MessageConstant;
import com.have.blog.xo.constant.SysConstant;
import com.have.blog.xo.service.admin.AdminRoleService;
import com.have.blog.xo.service.cache.AdminCacheService;
import com.have.blog.xo.vo.AdminRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
@SuppressWarnings("all")
public class AdminRoleServiceImpl implements AdminRoleService {
    @Autowired
    private TRoleMapper roleMapper;
    @Autowired
    private TAdminMapper adminMapper;
    @Autowired
    private AdminCacheService adminCacheService;

    @Override
    public String getList(AdminRoleVo roleVo) {
        PageHelper.startPage(roleVo.getCurrentPage(), roleVo.getPageSize());
        TRoleExample example = new TRoleExample();
        TRoleExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(roleVo.getKeyword()) && !StringUtils.isEmpty(roleVo.getKeyword().trim())) {
            criteria.andRoleNameLike(roleVo.getKeyword());
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        List<TRole> roleList = roleMapper.selectByExample(example);
        return ResultUtils.result(SysConstant.SUCCESS, CommonPage.restPage(roleList));
    }

    @Override
    public String add(AdminRoleVo roleVo) {
        String roleName = roleVo.getRoleName();
        TRoleExample roleExample = new TRoleExample();
        roleExample.createCriteria()
                .andRoleNameEqualTo(roleName);
        List<TRole> tRoles = roleMapper.selectByExample(roleExample);
        if (!CollectionUtils.isEmpty(tRoles)) {
            return ResultUtils.errorWithMessage(MessageConstant.ROLENAME_EXIST);
        }
        TRole role = new TRole();
        role.setRoleName(roleName);
        role.setSummary(roleVo.getSummary());
        role.setStatus(BlogStatus.ENABLE.getStatus());
        role.setUid(MyStringUtils.getUUID());
        role.setCategoryMenuUids(roleVo.getCategoryMenuUids());
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        roleMapper.insertSelective(role);
        return ResultUtils.successWithMessage(MessageConstant.INSERT_SUCCESS);
    }

    @Override
    public String edit(AdminRoleVo roleVo) {
        String roleName = roleVo.getRoleName();
        TRoleExample roleExample = new TRoleExample();
        roleExample.createCriteria()
                .andRoleNameEqualTo(roleName);
        List<TRole> tRoles = roleMapper.selectByExample(roleExample);
        if (!CollectionUtils.isEmpty(tRoles) && !tRoles.get(0).getUid().equals(roleVo.getUid())) {
            return ResultUtils.errorWithMessage(MessageConstant.ROLENAME_EXIST);
        }
        TRole role = new TRole();
        role.setRoleName(roleName);
        role.setSummary(roleVo.getSummary());
        role.setStatus(BlogStatus.ENABLE.getStatus());
        role.setCategoryMenuUids(roleVo.getCategoryMenuUids());
        role.setUpdateTime(new Date());
        role.setUid(roleVo.getUid());
        roleMapper.updateByPrimaryKeySelective(role);
        // 更新权限后需要删除缓存中的权限
        adminCacheService.deleteResourceList("*");
        return ResultUtils.successWithMessage(MessageConstant.UPDATE_SUCCESS);
    }

    @Override
    public String delete(AdminRoleVo roleVo) {
        String uid = roleVo.getUid();

        TAdminExample adminExample = new TAdminExample();
        adminExample.createCriteria()
                .andRoleUidEqualTo(uid);
        List<TAdmin> adminList = adminMapper.selectByExample(adminExample);
        if (!CollectionUtils.isEmpty(adminList)) {
            return ResultUtils.errorWithMessage(MessageConstant.ADMIN_UNDER_THIS_ROLE);
        }

        TRole role = new TRole();
        role.setUid(uid);
        role.setStatus(BlogStatus.DISABLE.getStatus());
        role.setUpdateTime(new Date());
        roleMapper.updateByPrimaryKeySelective(role);
        // 更新权限后需要删除缓存中的权限
        adminCacheService.deleteResourceList("*");
        return ResultUtils.successWithMessage(MessageConstant.DELETE_SUCCESS);
    }

    @Override
    public TRole getRoleByUid(String uid) {
        TRole role = roleMapper.selectByPrimaryKey(uid);
        if (role != null) {
            return role;
        }
        return new TRole();
    }
}
